package com.tools;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Environment;

import com.bean.Defaults;
import com.bean.Pics;
import com.bean.TopicColumns;
import com.bean.TopicData;
import com.bean.TopicFeed;
import com.bean.TopicNews;
import com.bean.feeds;
import com.bean.paramz;

public class TopicJosn {

	public paramz josn(String result) throws JSONException {

		JSONObject obj = new JSONObject(result);
		JSONObject paramzobj = obj.getJSONObject("paramz");

		JSONObject topicobj = paramzobj.getJSONObject("topic");
		TopicNews topic = new TopicNews();
		topic.setCategory(topicobj.getString("category"));
		topic.setChanged(topicobj.getString("changed"));
		topic.setContents(topicobj.getString("contents"));
		topic.setCover(topicobj.getString("cover"));
		topic.setCreated(topicobj.getString("created"));
		topic.setId(topicobj.getString("id"));
		topic.setPhoto(topicobj.getString("photo"));
		topic.setSubject(topicobj.getString("subject"));
		topic.setSubtitle(topicobj.getString("subtitle"));

		JSONArray defaultsarray = paramzobj.getJSONArray("defaults");
		ArrayList<Defaults> defaultslist = new ArrayList<Defaults>();

		for (int i = 0; i < defaultsarray.length(); i++) {
			JSONObject defaultsobj = defaultsarray.getJSONObject(i);
			TopicColumns columns1 = new TopicColumns();

			Defaults defaults = new Defaults();
			defaults.setCategory(defaultsobj.getString("category"));
			defaults.setId(defaultsobj.getString("id"));
			defaults.setOid(defaultsobj.getString("oid"));
			JSONObject dataobj = defaultsobj.getJSONObject("data");
			TopicData data = new TopicData();
			data.setChanged(dataobj.getString("changed"));
			data.setCover(dataobj.getString("cover"));
			data.setFormat(dataobj.getString("format"));
			data.setSubject(dataobj.getString("subject"));
			data.setSummary(dataobj.getString("summary"));
			defaults.setData(data);
			defaultslist.add(defaults);

		}

		JSONArray columnsarray = paramzobj.getJSONArray("columns");
		ArrayList<TopicColumns> columnslist = new ArrayList<TopicColumns>();
		for (int i = 0; i < columnsarray.length(); i++) {
			TopicColumns columns = new TopicColumns();
			JSONObject columnsobj = columnsarray.getJSONObject(i);
			columns.setName(columnsobj.getString("name"));
			JSONArray feedarray = columnsobj.getJSONArray("feed");
			ArrayList<TopicFeed> feedlist = new ArrayList<TopicFeed>();
			for (int j = 0; j < feedarray.length(); j++) {
				TopicFeed feed = new TopicFeed();

				JSONObject feedobj = feedarray.getJSONObject(j);

				feed.setCategory(feedobj.getString("category"));

				feed.setId(feedobj.getString("id"));
				feed.setOid(feedobj.getString("oid"));

				TopicData data1 = new TopicData();

				JSONObject dataobj = feedobj.getJSONObject("data");

				data1.setChanged(dataobj.getString("changed"));
				data1.setCover(dataobj.getString("cover"));
				data1.setFormat(dataobj.getString("format"));
				data1.setSubject(dataobj.getString("subject"));
				data1.setSummary(dataobj.getString("summary"));
				feed.setData(data1);
				if (dataobj.getString("format").equals("map")) {
					JSONArray picsarray = dataobj.getJSONArray("pics");
					ArrayList<Pics> picslist = new ArrayList<Pics>();
					for (int k = 0; k < picsarray.length(); k++) {
						Pics pics = new Pics();
						JSONObject picsobj = picsarray.getJSONObject(k);
						pics.setId(picsobj.getString("id"));
						pics.setPhoto(picsobj.getString("photo"));
						pics.setSubject(picsobj.getString("subject"));
						picslist.add(pics);
					}
					data1.setPics(picslist);
				}
				feedlist.add(feed);
			}

			columns.setFeeds(feedlist);
			columnslist.add(columns);

		}

		paramz paramzs = new paramz();
		paramzs.setTopic(topic);
		paramzs.setDefaults(defaultslist);
		paramzs.setColumns(columnslist);

		return paramzs;

	}

}
